package com.daon.service.impl;

import com.daon.common.ServerResponse;
import com.daon.dao.SysRoleMapper;
import com.daon.dao.SysRoleNodeMapper;
import com.daon.dao.SysRoleUserMapper;
import com.daon.param.SysRoleParam;
import com.daon.pojo.SysRole;
import com.daon.pojo.SysRoleNode;
import com.daon.pojo.SysRoleUser;
import com.daon.service.ISysRoleService;
import com.daon.util.BeanValidatorUtil;
import com.daon.vo.SysRoleNodeVo;
import com.google.common.base.Splitter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

/**
 * @Author 1daon
 * @EMAIL 532418116@qq.com
 * @Version 创建时间：2019/3/5 12:45 PM
 * @Modified By
 */
@Service("iSysRoleService")
public class SysRoleServiceImpl implements ISysRoleService{
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private SysRoleNodeMapper sysRoleNodeMapper;
    @Override
    public List<SysRole> sysRoleListBySiteId() {
        List<SysRole> sysRoleList = sysRoleMapper.sysRoleList();
        return sysRoleList;
    }

    @Override
    public ServerResponse<SysRole> save(SysRoleParam sysRoleParam) {
        BeanValidatorUtil.check(sysRoleParam);
        SysRole sysRole = SysRole.builder().roleName(sysRoleParam.getRoleName()).roleDesc(sysRoleParam.getRoleDesc())
                .createdAt(new Date()).updatedAt(new Date()).build();
        int rowCount = sysRoleMapper.insertSelective(sysRole);
        if(rowCount > 0) return ServerResponse.createBySuccessMessage("添加角色成功!");
        return ServerResponse.createByErrorMessage("添加角色失败!");
    }

    @Override
    public ServerResponse updateDeleteSysPostById(int id) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        if(sysRole == null) return ServerResponse.createByErrorMessage("角色信息不存在！");
        int resultCount = sysRoleMapper.deleteByPrimaryKey(id);
        if(resultCount > 0) return ServerResponse.createBySuccessMessage("删除成功！");
        return ServerResponse.createByErrorMessage("删除失败！");
    }

    @Override
    public ServerResponse update(SysRoleParam sysRoleParam) {
        BeanValidatorUtil.check(sysRoleParam);
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(sysRoleParam.getRoleId());
        if(sysRole == null) return ServerResponse.createByErrorMessage("角色信息不存在！");
        SysRole updateSysRole = SysRole.builder().roleName(sysRoleParam.getRoleName()).roleDesc(sysRoleParam.getRoleDesc())
                .roleId(sysRoleParam.getRoleId()).build();
        int updateCount = sysRoleMapper.updateByPrimaryKeySelective(updateSysRole);
        if(updateCount > 0){
            return ServerResponse.createBySuccessMessage("更新角色信息成功！");
        }
        return ServerResponse.createByErrorMessage("更新角色信息失败！");
    }

    @Override
    public SysRole getSysRoleById(int id) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        return sysRole;
    }

    @Override
    public List<SysRoleNodeVo> getRoleNodeByRoleId(Integer roleId) {
        List<SysRoleNodeVo> sysSiteNodeVoList = sysRoleNodeMapper.getRoleNodeListByRoleId(roleId);
        return sysSiteNodeVoList;
    }

    @Override
    public ServerResponse auth(int roleId, int[] nodeIds) {
        if(nodeIds.length == 0) return ServerResponse.createByErrorMessage("请选择要分配的权限！");
        if(roleId == 0) return ServerResponse.createByErrorMessage("参数错误！");
        List<SysRoleNode> sysRoleNodes = sysRoleNodeMapper.selectByRoleId(roleId);
        if(CollectionUtils.isNotEmpty(sysRoleNodes)) sysRoleNodeMapper.deleteByRoleId(roleId);
        List<SysRoleNode> sysRoleNodeList = new ArrayList<>();
        for (int i = 0; i < nodeIds.length; i++) {
            SysRoleNode sysRoleNode = SysRoleNode.builder().roleId(roleId).nodeId(nodeIds[i]).build();
            sysRoleNodeList.add(sysRoleNode);
        }
        int rowCount = sysRoleNodeMapper.insertRoleNodeBatch(sysRoleNodeList);
        if (rowCount > 0) return ServerResponse.createBySuccessMessage("分配权限成功！");
        return ServerResponse.createByErrorMessage("分配权限失败！");
    }

    @Override
    public ServerResponse assignUserRole(int roleId, String userIdStr) {
        if(roleId == 0) ServerResponse.createByErrorMessage("参数错误");
        if(StringUtils.isBlank(userIdStr)) ServerResponse.createByErrorMessage("请选择要分配的用户");
        sysRoleUserMapper.deleteByRoleId(roleId);
        List<String> userIds = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(userIdStr);
        List<SysRoleUser> sysRoleUserList = new ArrayList<>();
        for(int i=0;i<userIds.size();i++){
            int uid = Integer.parseInt(userIds.get(i));
            SysRoleUser sysRoleUser = SysRoleUser.builder().roleId(roleId).userId(uid).build();
            sysRoleUserList.add(sysRoleUser);
        }
        int rowCount = sysRoleUserMapper.insertByBatch(sysRoleUserList);
        if(rowCount <= 0) return ServerResponse.createByErrorMessage("分配用户失败");
        return ServerResponse.createBySuccessMessage("分配成功");
    }
}
